﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CommonControl;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmUnitBase : XtraForm
    {
        public FrmUnitBase()
        {
            InitializeComponent();
            InitGrid();
        }
        private void FrmUnitBase_Load(object sender, EventArgs e)
        {
            LoadData();
        }


        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frmNew = new FrmUnitBase_Editor();
            frmNew.UnitBaseID = 0;
            frmNew.ShowDialog();
        }
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvUnitBase.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmUnitBase_Editor();
            frm.UnitBaseID = Convert.ToInt32(grvUnitBase.GetFocusedDataRow()["UnitBaseID"]);
            frm.ShowDialog();
        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvUnitBase.GetFocusedDataRow() == null)
            {
                return;
            }
            if (DialogResult.No == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }
            var bll = new DMZS.BLL.UnitBase();
            bll.DeleteExpand(Convert.ToInt32(grvUnitBase.GetFocusedDataRow()["UnitBaseID"]));
            LoadData();
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            var userBll = new DMZS.BLL.UnitBase();
            try
            {
                grdUnitBase.DataSource = userBll.GetListForUIExpand(string.Empty).Tables[0];
            }
            catch (Exception ex)
            {
                Utils.WriteTxtLog(ex);
                XtraMessageBox.Show(Utils.GetRuntimeMessage(ex), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvUnitBase.IndicatorWidth = grvUnitBase.IndicatorWidth == -1 ? 40 : grvUnitBase.IndicatorWidth;
            grvUnitBase.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvUnitBase);
        }

        private void grvUnitBase_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }

        private void grvUnitBase_DoubleClick(object sender, EventArgs e)
        {
            if (grvUnitBase.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmUnitBase_Editor();
            frm.UnitBaseID = Convert.ToInt32(grvUnitBase.GetFocusedDataRow()["UnitBaseID"]);
            frm.ShowDialog();
        }
    }
}
